package servlet;

import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年09月02日10:07
 */
@WebServlet("/showModifyEmp")
public class ShowModifyEmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id=request.getParameter("id");
        System.out.println("id:"+id);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String url="jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn= DriverManager.getConnection(url,"root","root");
            String sql=new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .append(" where id=? ")
                    .toString();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(id));
            rs=ps.executeQuery();
            if(rs.next()){
                out.print("<!DOCTYPE html>");
                out.print("<html lang='en'>");
                out.print("<head>");
                out.print("<meta charset='UTF-8'>");
                out.print("<title>修改员工信息</title>");
                out.print("</head>");
                out.print("<body>");
                out.print("<h1>修改员工信息</h1>");
                out.print("<a href=''>返回员工列表</a>");
                out.print("<form action='modifyEmp' method='post'>");
                out.print(" 编号: <input type='text' name='id' value='"+rs.getInt("id")+"' readonly><br>");
                out.print("姓名: <input type='text' name='name' value='"+rs.getString("name")+"'><br>");
                out.print("工资: <input type='text' name='salary' value='"+rs.getDouble("salary")+"'><br>");
                out.print("性别: <input type='radio' name='gender' value='man' "+(rs.getString("gender").equals("man")?"checked":"")+">男");
                out.print("<input type='radio' name='gender' value='woman'"+(rs.getString("gender").equals("woman")?"checked":"")+">女");
                out.print("<br>");
                out.print("学历:");
                out.print("<select name='edu' >");
                out.print("<option value='-1'>请选择</option>");
                out.print(" <option value='1' "+((rs.getString("edu").equals("1")?"selected":""))+">高中</option>");
                out.print(" <option value='2' "+((rs.getString("edu").equals("2")?"selected":""))+">大专</option>");
                out.print(" <option value='3' "+((rs.getString("edu").equals("3")?"selected":""))+">本科</option>");
                out.print(" <option value='4' "+((rs.getString("edu").equals("4")?"selected":""))+">研究生</option>");
                out.print(" </select>");
                out.print(" <br>");
                out.print("兴趣");
                out.print(" <input type='checkbox' name='hobbies' value='eat' "+ StringUtil.checkHobby(rs.getString("hobbies"),"eat") +">吃饭");
                out.print(" <input type='checkbox' name='hobbies' value='sleep' "+ StringUtil.checkHobby(rs.getString("hobbies"),"sleep") +">睡觉");
                out.print(" <input type='checkbox' name='hobbies' value='beat' "+ StringUtil.checkHobby(rs.getString("hobbies"),"beat") +">打豆豆");
                out.print(" <br>");
                out.print(" <input type='submit' value='修改'>");
                out.print("</form>");
                out.print("</body>");
                out.print("</html>");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
